package com.shangguosha.factory;

import com.shangguosha.model.Card;
import com.shangguosha.model.cards.Kill;
import com.shangguosha.model.cards.Dodge;
import com.shangguosha.model.cards.Peach;
import com.shangguosha.model.enums.CardType;
import com.shangguosha.model.enums.Suit;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

/**
 * 卡牌工厂类
 * 负责创建和管理游戏中的所有卡牌
 */
@Slf4j
public class CardFactory {
    
    private static final Random random = new Random();
    
    /**
     * 创建标准108张牌库
     */
    public static List<Card> createStandardDeck() {
        List<Card> deck = new ArrayList<>();
        
        // 创建基本牌
        createBasicCards(deck);
        
        // 创建锦囊牌 (TODO: 后续实现)
        // createTrickCards(deck);
        
        // 创建装备牌 (TODO: 后续实现)
        // createEquipmentCards(deck);
        
        // 洗牌
        Collections.shuffle(deck);
        
        log.info("创建了标准牌库，共 {} 张牌", deck.size());
        return deck;
    }
    
    /**
     * 创建基本牌
     */
    private static void createBasicCards(List<Card> deck) {
        // 杀 - 30张
        createKillCards(deck);
        
        // 闪 - 15张
        createDodgeCards(deck);
        
        // 桃 - 8张
        createPeachCards(deck);
    }
    
    /**
     * 创建杀牌
     */
    private static void createKillCards(List<Card> deck) {
        // 黑桃杀
        deck.add(new Kill("kill_s7", "杀", Suit.SPADE, 7));
        deck.add(new Kill("kill_s8", "杀", Suit.SPADE, 8));
        deck.add(new Kill("kill_s8_2", "杀", Suit.SPADE, 8));
        deck.add(new Kill("kill_s9", "杀", Suit.SPADE, 9));
        deck.add(new Kill("kill_s9_2", "杀", Suit.SPADE, 9));
        deck.add(new Kill("kill_s10", "杀", Suit.SPADE, 10));
        deck.add(new Kill("kill_s10_2", "杀", Suit.SPADE, 10));
        
        // 梅花杀
        deck.add(new Kill("kill_c2", "杀", Suit.CLUB, 2));
        deck.add(new Kill("kill_c3", "杀", Suit.CLUB, 3));
        deck.add(new Kill("kill_c4", "杀", Suit.CLUB, 4));
        deck.add(new Kill("kill_c5", "杀", Suit.CLUB, 5));
        deck.add(new Kill("kill_c6", "杀", Suit.CLUB, 6));
        deck.add(new Kill("kill_c7", "杀", Suit.CLUB, 7));
        deck.add(new Kill("kill_c8", "杀", Suit.CLUB, 8));
        deck.add(new Kill("kill_c8_2", "杀", Suit.CLUB, 8));
        deck.add(new Kill("kill_c9", "杀", Suit.CLUB, 9));
        deck.add(new Kill("kill_c9_2", "杀", Suit.CLUB, 9));
        deck.add(new Kill("kill_c10", "杀", Suit.CLUB, 10));
        deck.add(new Kill("kill_c10_2", "杀", Suit.CLUB, 10));
        deck.add(new Kill("kill_cj", "杀", Suit.CLUB, 11));
        deck.add(new Kill("kill_cj_2", "杀", Suit.CLUB, 11));
        
        // 方片杀
        deck.add(new Kill("kill_d6", "杀", Suit.DIAMOND, 6));
        deck.add(new Kill("kill_d7", "杀", Suit.DIAMOND, 7));
        deck.add(new Kill("kill_d8", "杀", Suit.DIAMOND, 8));
        deck.add(new Kill("kill_d9", "杀", Suit.DIAMOND, 9));
        deck.add(new Kill("kill_d10", "杀", Suit.DIAMOND, 10));
        deck.add(new Kill("kill_d13", "杀", Suit.DIAMOND, 13));
        
        // 红桃杀
        deck.add(new Kill("kill_h10", "杀", Suit.HEART, 10));
        deck.add(new Kill("kill_h10_2", "杀", Suit.HEART, 10));
        deck.add(new Kill("kill_h11", "杀", Suit.HEART, 11));
    }
    
    /**
     * 创建闪牌
     */
    private static void createDodgeCards(List<Card> deck) {
        // 红桃闪
        deck.add(new Dodge("dodge_h2", "闪", Suit.HEART, 2));
        deck.add(new Dodge("dodge_h2_2", "闪", Suit.HEART, 2));
        deck.add(new Dodge("dodge_h13", "闪", Suit.HEART, 13));
        
        // 方片闪
        deck.add(new Dodge("dodge_d2", "闪", Suit.DIAMOND, 2));
        deck.add(new Dodge("dodge_d2_2", "闪", Suit.DIAMOND, 2));
        deck.add(new Dodge("dodge_d3", "闪", Suit.DIAMOND, 3));
        deck.add(new Dodge("dodge_d4", "闪", Suit.DIAMOND, 4));
        deck.add(new Dodge("dodge_d5", "闪", Suit.DIAMOND, 5));
        deck.add(new Dodge("dodge_d6", "闪", Suit.DIAMOND, 6));
        deck.add(new Dodge("dodge_d7", "闪", Suit.DIAMOND, 7));
        deck.add(new Dodge("dodge_d8", "闪", Suit.DIAMOND, 8));
        deck.add(new Dodge("dodge_d9", "闪", Suit.DIAMOND, 9));
        deck.add(new Dodge("dodge_d10", "闪", Suit.DIAMOND, 10));
        deck.add(new Dodge("dodge_d11", "闪", Suit.DIAMOND, 11));
        deck.add(new Dodge("dodge_d11_2", "闪", Suit.DIAMOND, 11));
    }
    
    /**
     * 创建桃牌
     */
    private static void createPeachCards(List<Card> deck) {
        // 红桃桃
        deck.add(new Peach("peach_h3", "桃", Suit.HEART, 3));
        deck.add(new Peach("peach_h4", "桃", Suit.HEART, 4));
        deck.add(new Peach("peach_h6", "桃", Suit.HEART, 6));
        deck.add(new Peach("peach_h7", "桃", Suit.HEART, 7));
        deck.add(new Peach("peach_h8", "桃", Suit.HEART, 8));
        deck.add(new Peach("peach_h9", "桃", Suit.HEART, 9));
        deck.add(new Peach("peach_h12", "桃", Suit.HEART, 12));
        
        // 方片桃
        deck.add(new Peach("peach_d12", "桃", Suit.DIAMOND, 12));
    }
    
    /**
     * 创建测试牌库（简化版）
     */
    public static List<Card> createTestDeck() {
        List<Card> deck = new ArrayList<>();
        
        // 创建简化的测试牌库
        for (int i = 1; i <= 10; i++) {
            Suit suit = Suit.values()[i % 4];
            int number = (i % 13) + 1;
            
            // 杀牌
            deck.add(new Kill("test_kill_" + i, "杀", suit, number));
            
            // 闪牌
            deck.add(new Dodge("test_dodge_" + i, "闪", suit, number));
            
            // 桃牌
            deck.add(new Peach("test_peach_" + i, "桃", suit, number));
        }
        
        Collections.shuffle(deck);
        log.info("创建了测试牌库，共 {} 张牌", deck.size());
        return deck;
    }
    
    /**
     * 创建指定类型的卡牌
     */
    public static Card createCard(CardType cardType, String id, String name, Suit suit, int number) {
        switch (cardType) {
            case BASIC:
                return createBasicCard(id, name, suit, number);
            case TRICK:
                // TODO: 实现锦囊牌创建
                log.warn("锦囊牌暂未实现: {}", name);
                return null;
            case EQUIPMENT:
                // TODO: 实现装备牌创建
                log.warn("装备牌暂未实现: {}", name);
                return null;
            default:
                log.warn("未知卡牌类型: {}", cardType);
                return null;
        }
    }
    
    /**
     * 创建基本牌
     */
    private static Card createBasicCard(String id, String name, Suit suit, int number) {
        switch (name) {
            case "杀":
                return new Kill(id, name, suit, number);
            case "闪":
                return new Dodge(id, name, suit, number);
            case "桃":
                return new Peach(id, name, suit, number);
            default:
                log.warn("未知基本牌: {}", name);
                return null;
        }
    }
    
    /**
     * 创建随机卡牌
     */
    public static Card createRandomCard() {
        String[] cardNames = {"杀", "闪", "桃"};
        Suit[] suits = Suit.values();
        
        String name = cardNames[random.nextInt(cardNames.length)];
        Suit suit = suits[random.nextInt(suits.length)];
        int number = random.nextInt(13) + 1;
        String id = "random_" + System.currentTimeMillis() + "_" + random.nextInt(1000);
        
        return createBasicCard(id, name, suit, number);
    }
    
    /**
     * 创建指定数量的随机卡牌
     */
    public static List<Card> createRandomCards(int count) {
        List<Card> cards = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            Card card = createRandomCard();
            if (card != null) {
                cards.add(card);
            }
        }
        return cards;
    }
    
    /**
     * 根据花色和点数创建卡牌
     */
    public static Card createCardBySuitAndNumber(Suit suit, int number) {
        // 根据花色和点数的组合决定卡牌类型
        String id = "auto_" + suit.name().toLowerCase() + "_" + number;
        
        // 简化的卡牌分配逻辑
        if (suit == Suit.SPADE || suit == Suit.CLUB) {
            // 黑色花色主要是杀
            if (number >= 2 && number <= 10) {
                return new Kill(id, "杀", suit, number);
            } else if (number == 1 || number == 11) {
                return new Dodge(id, "闪", suit, number);
            } else {
                return new Peach(id, "桃", suit, number);
            }
        } else {
            // 红色花色主要是闪和桃
            if (number >= 2 && number <= 9) {
                return new Dodge(id, "闪", suit, number);
            } else if (number >= 10 && number <= 13) {
                return new Peach(id, "桃", suit, number);
            } else {
                return new Kill(id, "杀", suit, number);
            }
        }
    }
    
    /**
     * 创建完整的52张扑克牌对应的游戏卡牌
     */
    public static List<Card> createFullDeck() {
        List<Card> deck = new ArrayList<>();
        
        for (Suit suit : Suit.values()) {
            for (int number = 1; number <= 13; number++) {
                Card card = createCardBySuitAndNumber(suit, number);
                if (card != null) {
                    deck.add(card);
                }
            }
        }
        
        Collections.shuffle(deck);
        log.info("创建了完整牌库，共 {} 张牌", deck.size());
        return deck;
    }
}